//
// Copyright 2021 Bloomberg Finance L.P.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
@testable import XCDiffCore
import XCTest

final class SwiftPackageDescriptorTests: XCTestCase {
    func test_differenceFrom() {
        // Given
        let subject = SwiftPackageDescriptor(
            name: "PackageA",
            url: "https://url.to/package-a.git",
            version: ".exact(1.0.0)"
        )

        let others: [SwiftPackageDescriptor] = [
            SwiftPackageDescriptor(
                name: "PackageA",
                url: "https://url.to/package-a.git",
                version: ".exact(1.0.0)"
            ),
            SwiftPackageDescriptor(
                name: "PackageA", url: "https://url.to/package-a.git", version: ".upToNextMajor(from: 1.0.0)"
            ),
            SwiftPackageDescriptor(
                name: "PackageA", url: "https://url.to/package-a-different.git", version: ".exact(1.0.0)"
            ),
            SwiftPackageDescriptor(
                name: "PackageA1", url: "https://url.to/package-a.git", version: ".exact(1.0.0)"
            ),
            SwiftPackageDescriptor(
                name: "PackageA1", url: "https://url.to/package-a.git", version: ".branch(main)"
            ),
            SwiftPackageDescriptor(
                name: "PackageA1", url: "https://url.to/package-a-different.git", version: ".branch(main)"
            ),
        ]

        // When
        let subjectDiffFromOther = others.map {
            subject.difference(from: $0)
        }
        let otherDiffFromSubject = others.map {
            $0.difference(from: subject)
        }

        // Then
        XCTAssertEqual(subjectDiffFromOther, [
            nil,
            "version = .exact(1.0.0)",
            "url = https://url.to/package-a.git",
            "name = PackageA",
            "name = PackageA, version = .exact(1.0.0)",
            "name = PackageA, url = https://url.to/package-a.git, version = .exact(1.0.0)",
        ])
        XCTAssertEqual(otherDiffFromSubject, [
            nil,
            "version = .upToNextMajor(from: 1.0.0)",
            "url = https://url.to/package-a-different.git",
            "name = PackageA1",
            "name = PackageA1, version = .branch(main)",
            "name = PackageA1, url = https://url.to/package-a-different.git, version = .branch(main)",
        ])
    }
}
